﻿using System;
using System.Collections.Generic;
using System.Data;
using Loki.Data;
using Luna.Data.Configuration;

namespace Luna.Data.CRM
{
    /// <summary>
    /// Mapper for data interface IContactData.
    /// </summary>
    public partial class ContactDataMapper : LunaEntityMapper<ContactData, IContactData> 
    {
        public ContactDataMapper()
        {
            GetAllRequest = "Crm.contact_select_all";
            GetRequest = "Crm.contact_select";
            InsertRequest = "Crm.contact_insert";
            UpdateRequest = "Crm.contact_update";
            DeleteRequest = "Crm.contact_delete";
            GetAllByAccountIDRequest =  "Crm.contact_select_by_accountid";
        }
        
        private const string AccountIDColumnName = "AccountID";
        private const string TitleColumnName = "Title";
        private const string NameColumnName = "Name";
        private const string GivenNameColumnName = "GivenName";
        private const string FunctionColumnName = "Function";
        private const string Phone1ColumnName = "Phone1";
        private const string Phone2ColumnName = "Phone2";
        private const string FaxColumnName = "Fax";
        private const string MailColumnName = "Mail";
        private const string AddressColumnName = "Address";
        private const string ZipCodeColumnName = "ZipCode";
        private const string CityColumnName = "City";
        private const string CountryColumnName = "Country";
        private const string UseAccountInfosColumnName = "UseAccountInfos";
        public string GetAllByAccountIDRequest {get; set;}

        protected override void LoadCoreData(IContactData entity, IDataRecord record)
        {
            entity.AccountID = record.GetGuid(AccountIDColumnName);
            entity.Title = record.GetString(TitleColumnName);
            entity.Name = record.GetString(NameColumnName);
            entity.GivenName = record.GetString(GivenNameColumnName);
            entity.Function = record.GetString(FunctionColumnName);
            entity.Phone1 = record.GetString(Phone1ColumnName);
            entity.Phone2 = record.GetString(Phone2ColumnName);
            entity.Fax = record.GetString(FaxColumnName);
            entity.Mail = record.GetString(MailColumnName);
            entity.Address = record.GetString(AddressColumnName);
            entity.ZipCode = record.GetString(ZipCodeColumnName);
            entity.City = record.GetString(CityColumnName);
            entity.Country = record.GetString(CountryColumnName);
            entity.UseAccountInfos = record.GetBoolean(UseAccountInfosColumnName);
        }
        
        protected override void MapSaveParameters(IDatabaseCommand command, IContactData entity)
        {
            command.AddParameter(AccountIDColumnName, entity.AccountID);
            command.AddParameter(TitleColumnName, entity.Title);
            command.AddParameter(NameColumnName, entity.Name);
            command.AddParameter(GivenNameColumnName, entity.GivenName);
            command.AddParameter(FunctionColumnName, entity.Function);
            command.AddParameter(Phone1ColumnName, entity.Phone1);
            command.AddParameter(Phone2ColumnName, entity.Phone2);
            command.AddParameter(FaxColumnName, entity.Fax);
            command.AddParameter(MailColumnName, entity.Mail);
            command.AddParameter(AddressColumnName, entity.Address);
            command.AddParameter(ZipCodeColumnName, entity.ZipCode);
            command.AddParameter(CityColumnName, entity.City);
            command.AddParameter(CountryColumnName, entity.Country);
            command.AddParameter(UseAccountInfosColumnName, entity.UseAccountInfos);
            command.AddParameter(IDParameterName, entity.PK);
        }
           
        
        public IEnumerable<IContactData> GetAllByAccountID(IDbTransaction transaction, Guid accountID)
        {
            return ExecuteGetAll(
                    transaction,
                    GetAllByAccountIDRequest,
                    c => { 
                            c.AddParameter(AccountIDColumnName, accountID); 
                        });
        }
    }
}